package com.example.cms.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import com.example.cms.entity.*;
import com.example.cms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**
 * 测试控制器
 *
 * @author: ???
 * @create: 2018-05-08-下午 16:46
 */

@RestController
public class HelloController {
	
	
	@Autowired
	private TestService testService;
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private OrderService orderService;

	@Autowired
	private ConsultantService consultantService;

	@Autowired
	private QuestionnairedService questionnairedService;
	
	@RequestMapping("/hello")
    public List<Test> hello() {
        return testService.getTest();
    }
	
	@RequestMapping("/cus")
    public List<Customer> cus() {
        return customerService.getAllCus();
    }
	
	@RequestMapping("/pagecount")
    public Integer cusPageNum() {
        return customerService.countPageSize();
    }

    @RequestMapping("/allQues")
    public List<Questionnaired> getQues(){
		return questionnairedService.getAllQues();
	}

	@RequestMapping("/getConu")
	public Consultant getConu(Integer conId){//这里
		return consultantService.getConsultantAndCourses(conId);
	}

	@RequestMapping("/pages")
    public List<Customer> cusPage(String page) {
		//int page = 1;
		System.out.println("page:"+page);
        return customerService.getByPageCus(Integer.parseInt(page));
    }

	@RequestMapping("/cusAndOr")
	public List<Customer> getCusAndor() {
		return customerService.getAllCusAndOr();
	}
	
	@RequestMapping("/orders")
    public List<Order> getorders() {
		//int page = 1;
		
        return orderService.getAllorderS();
    }

    @RequestMapping(value = "insertOrder",method = RequestMethod.GET)
    public Integer insertOrder(Order order){//insertOrder?orderName=jimo&orderCout=10&orderMoney=90.22&customerId=6
		System.out.println(order.toString());;
		return orderService.insertOrder(order);
	}
	  
	@RequestMapping(value="/testUpload",method=RequestMethod.POST)  
	public String testUploadFile(@RequestParam("file")MultipartFile[] files,HttpServletRequest request) {  
		System.out.println("IP:{"+request.getRemoteAddr()+"},进行文件上传——开始,");
		int count = 0; //文件数统计
		for(int i=0;i<files.length;i++) {
			if (!files[i].isEmpty()) {
				try {
					System.out.print("第{"+(i+1)+"}个文件开始上传,");
					// 使用UUID确保上传文件不重复
					BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(new File("D:\\图片\\" + UUID.randomUUID() + "-" +files[i].getOriginalFilename())));
					out.write(files[i].getBytes()); 
					out.flush();
					out.close();
					System.out.println("第{"+(i+1)+"}个文件上传成功");
					count++;
				} catch (Exception e) {
					
				} 
			}
		}
		System.out.println(("IP:{"+request.getRemoteAddr()+"},共{"+count+"}个文件成功上传——结束,"));
		 return "文件成功上传:"+count;		
	}
}
